package com.czmh.etc.onlinepubsys.utils;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;

/**
 * 
 * @描述：OSS工具类
 * @author： Seven.
 * @创建日期：Dec 10, 2019 11:39:34 AM.
 * @版本：1.0.0
 */
@Component
public class AliyunOssUtil {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.baseUrl}")
    private String baseUrl;

    private OSS    ossClient;

    private OSS getInstance() {
        if (ossClient == null) {
            synchronized (AliyunOssUtil.class) {
                if (ossClient == null) {
                    ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                }
            }
        }
        return ossClient;
    }

    /**
     * 上传文件
     * 
     * @param inputStream     文件输入流
     * @param fileName        文件名
     * @return
     */
    public String upload(InputStream inputStream, String fileName) {
        try {
            getInstance().putObject(bucketName, fileName, inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return baseUrl + fileName;
    }
}